package com.doctor.windflower_doctor.activity;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.SystemApplication;
import com.doctor.windflower_doctor.actionBeen.DoctorAction;
import com.doctor.windflower_doctor.db.SharedPreferencesDB;
import com.doctor.windflower_doctor.http.callback.HttpRequestCallBack;
import com.doctor.windflower_doctor.http.parser.impl.JsonParser;
import com.doctor.windflower_doctor.http.request.HttpRequest;
import com.doctor.windflower_doctor.http.request.HttpRequestParams;
import com.doctor.windflower_doctor.http.response.HttpResponseInfo;
import com.doctor.windflower_doctor.util.ActivityStack;
import com.doctor.windflower_doctor.util.Check;
import com.doctor.windflower_doctor.util.Constent;
import com.doctor.windflower_doctor.view.LoadingDialog;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class Login extends BaseActivity implements Constent {
	private Button login;
	private ImageButton back_imageButton;
	private EditText phone_email_editText;
	private EditText password_editText;
	private TextView register_now_textView;
	private TextView Retrieve_password_textView;
	private SharedPreferencesDB db;
	private Dialog dialog;
	private LoadingDialog loadingDialog;
	private boolean gone;
	private boolean backs = false;

	@Override
	protected void getSaveData(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void processLogic() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (phone_email_editText.getText().toString().trim().equals("")) {
					Toast.makeText(Login.this, "请填写邮箱或手机号", Toast.LENGTH_SHORT)
							.show();
				} else if (password_editText.getText().toString().trim()
						.equals("")) {
					Toast.makeText(Login.this, "请填写密码", Toast.LENGTH_SHORT)
							.show();
				} else if (Check.isMobileNO(phone_email_editText.getText()
						.toString().trim())) {
					phone_Emial_Login("1");

				} else if (Check.isEmail(phone_email_editText.getText()
						.toString().trim())) {
					phone_Emial_Login("0");

				} else {
					Toast.makeText(Login.this, "请填写正确的手机号或邮箱号",
							Toast.LENGTH_SHORT).show();
				}

			}
		});
		back_imageButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		register_now_textView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				View view = LayoutInflater.from(Login.this).inflate(
						R.layout.call_view, null);
				Button make_sure_button = (Button) view
						.findViewById(R.id.call_button);
				Button cancel_button = (Button) view
						.findViewById(R.id.cancel_button);
				make_sure_button.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO
						// Auto-generated
						// method stub
						Uri uri = Uri.parse("tel:" + "010-56277595");
						Intent intent = new Intent();
						intent.setAction(Intent.ACTION_DIAL);
						intent.setData(uri);
						startActivity(intent);
						dialog.dismiss();
					}
				});
				cancel_button.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO
						// Auto-generated
						// method stub

						dialog.cancel();
					}
				});
				dialog.setContentView(view);
				dialog.show();

			}
		});

		Retrieve_password_textView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent(Login.this,
						AccountAuthentication.class));
			}
		});
	}

	private void phone_Emial_Login(String number) {
		HttpMethod requestMethod = HttpMethod.POST;
		HttpRequestCallBack<DoctorAction> requestCallBack = new HttpRequestCallBack<DoctorAction>(
				new JsonParser(), DoctorAction.class) {
			@Override
			protected void handleResult(
					HttpResponseInfo<DoctorAction> responseInfo) {
				if (responseInfo.success.success) {

					if (responseInfo.success.data.verify == 1) {
						SystemApplication.getInstance().rigistSoket();
						db.setPassword(password_editText.getText().toString()
								.trim());
						db.setIsLogin(true);
						db.setDocotorId(responseInfo.success.data.accountId);

						db.setUserName(responseInfo.success.data.username);
						db.setAccountName(responseInfo.success.data.accountName);
						db.setSingleId(responseInfo.success.data.id);
						db.putImage(responseInfo.success.data.imgUrl);
						db.setAcceptQuestion(responseInfo.success.data.stopService);
						startActivity(new Intent(Login.this, MainActivity.class));
						finish();

					} else if (responseInfo.success.data.verify == 0) {
						Toast.makeText(Login.this, "您还未认证", Toast.LENGTH_SHORT)
								.show();
					} else if (responseInfo.success.data.verify == 2) {
						Toast.makeText(Login.this, "待认证", Toast.LENGTH_SHORT)
								.show();
					} else {
						Toast.makeText(Login.this, "认证未通过", Toast.LENGTH_SHORT)
								.show();
					}
				} else {
					Toast.makeText(Login.this, responseInfo.success.message,
							Toast.LENGTH_SHORT).show();
				}
				loadingDialog.close();
			}

			@Override
			protected void handleError(HttpException e, String msg) {

				Toast.makeText(Login.this, "请检查网络状态" + msg, Toast.LENGTH_SHORT)
						.show();

				loadingDialog.close();
			}

			@Override
			public void onStart() {

				super.onStart();
				loadingDialog.show();
			}

		};
		HttpRequestParams params = new HttpRequestParams(requestMethod, URL
				+ LOGIN_URL, requestCallBack);
		params.addBodyParameter("username", phone_email_editText.getText()
				.toString().trim());
		params.addBodyParameter("password", password_editText.getText()
				.toString().trim());
		params.addBodyParameter("type", number);
		HttpRequest.send(params);

	}

	@Override
	protected void findViews() {
		// TODO Auto-generated method stub
		loadingDialog = new LoadingDialog(Login.this);
		dialog = new Dialog(Login.this);
		dialog.getWindow().setBackgroundDrawableResource(R.color.transparent);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		db = SharedPreferencesDB.getInstance(Login.this);
		Retrieve_password_textView = (TextView) findViewById(R.id.Retrieve_password_textView);
		register_now_textView = (TextView) findViewById(R.id.register_now_textView);
		back_imageButton = (ImageButton) findViewById(R.id.back_imageButton);
		login = (Button) findViewById(R.id.login_Button);
		password_editText = (EditText) findViewById(R.id.password_editText);
		phone_email_editText = (EditText) findViewById(R.id.phone_email_editText);
		if (db.getUserName() != null) {
			phone_email_editText.setText(db.getUserName());
		}

		backs = getIntent().getBooleanExtra("back", false);
		gone = getIntent().getBooleanExtra("gone", false);
		if (gone) {
			back_imageButton.setVisibility(View.GONE);
		}
	}

	@Override
	public int getResLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.login;
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		int keyCode = event.getKeyCode();
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0
				&& event.getAction() == KeyEvent.ACTION_DOWN) {
			if (backs) {
				if (MainActivity.instance != null) {
					MainActivity.isTrue = false;
					MainActivity.instance.finish();
				}
				ActivityStack.getInstance().finishAllActivity();
				System.exit(0);
			} else {
				finish();
			}
			return true;
		} else {
			return super.dispatchKeyEvent(event);
		}
	}
}
